﻿@page "/"
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@namespace Templates.TodoApp.Host.Pages
@using Stl.Fusion.Blazor
@using Templates.TodoApp.UI
@using Stl.Fusion.Server.Authentication
@using Stl.Fusion.Server.Endpoints
@inject ServerAuthHelper ServerAuthHelper
@inject BlazorCircuitContext BlazorCircuitContext
@{
    await ServerAuthHelper.UpdateAuthState(HttpContext);
    var authSchemas = await ServerAuthHelper.GetSchemas(HttpContext);
    var sessionId = ServerAuthHelper.Session.Id.Value;
    var isServerSideBlazor = BlazorModeEndpoint.IsBlazorServer(HttpContext);
    var isCloseWindowRequest = ServerAuthHelper.IsCloseWindowRequest(HttpContext, out var closeWindowFlowName);
    Layout = null;
}

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
    <title>TodoApp: Fusion+Blazor Template</title>
    <base href="~/" />
    <link href="css/app.css" rel="stylesheet" />
    <partial name="Blazorise.Head" />
    <script src="_content/Stl.Fusion.Blazor.Authentication/scripts/fusionAuth.js"></script>
    <script>
        window.FusionAuth.schemas = "@authSchemas";
    </script>
</head>
<body>
@if (isCloseWindowRequest) {
    <script>
        setTimeout(function () {
            window.close();
        }, 500)
    </script>
    <div class="alert alert-primary">
        @(closeWindowFlowName) completed, you can close this window.
    </div>
} else {
    <app id="app">
        @{
            using var prerendering = BlazorCircuitContext.Prerendering();
            var prerenderedApp = await Html.RenderComponentAsync<App>(
                isServerSideBlazor ? RenderMode.ServerPrerendered : RenderMode.WebAssemblyPrerendered,
                new { SessionId = sessionId });
        }
        @(prerenderedApp)
    </app>

    <div id="blazor-error-ui">
        <environment include="Staging,Production">
            An error has occurred. This application may no longer respond until reloaded.
        </environment>
        <environment include="Development">
            An unhandled exception has occurred. See browser dev tools for details.
        </environment>
        <a href="" class="reload">Reload</a>
        <a class="dismiss">🗙</a>
    </div>

    <partial name="Blazorise.Body" />
    <script type="module" src="./js/app.js"></script>
    <script src="_framework/blazor.@(isServerSideBlazor ? "server" : "webassembly").js"></script>
}
</body>
</html>
